package csx.study.demo.listener;

import com.rabbitmq.client.Channel;
import csx.study.demo.entity.VoucherOrder;
import csx.study.demo.service.IVoucherOrderService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class seckillvoucherListener {

    @Autowired
    IVoucherOrderService voucherOrderService;

    //默认串行消费
    @RabbitListener(bindings = @QueueBinding(

            value = @Queue(name = "direct.seckill.queue"),
            key = "direct.seckill",
            exchange = @Exchange(name = "lvan.direct", type = ExchangeTypes.DIRECT)
    )
    //,concurrency = "5" 如果要并行消费
    )

    public void recieveMessage(Message message, Channel channel, VoucherOrder voucherOrder) throws IOException {

        try {
            voucherOrderService.handleVoucherOrder(voucherOrder);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("监听到了"+message);
    }

 }
